package cn.wjssl.domain.rebate.service;

import cn.wjssl.domain.rebate.event.SendRebateMessageEvent;
import cn.wjssl.domain.rebate.model.aggregate.BehaviorRebateAggregate;
import cn.wjssl.domain.rebate.model.entity.BehaviorEntity;
import cn.wjssl.domain.rebate.model.entity.BehaviorRebateOrderEntity;
import cn.wjssl.domain.rebate.model.entity.TaskEntity;
import cn.wjssl.domain.rebate.model.valobj.DailyBehaviorRebateVO;
import cn.wjssl.domain.rebate.model.valobj.TaskStateVO;
import cn.wjssl.domain.rebate.repository.IBehaviorRebateRepository;
import cn.wjssl.types.common.Constants;
import cn.wjssl.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 用户行为返利实现类
 */
@Slf4j
@Service
public class BehaviorRebateService implements IBehaviorRebateService {

    @Resource
    private IBehaviorRebateRepository repository;
    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;


    /**
     * 用户行为(签到, 支付等)创建订单, mq消息入库
     * @param behaviorEntity 行为实体对象 {userId, BehaviorTypeVO, outBusinessNo}
     * 行为类型(BehaviorTypeVO)；sign 签到、openai_pay 支付
     * 业务ID(outBusinessNo)；签到则是日期字符串，支付则是外部的业务ID
     * @return 订单ID列表
     */
    @Override
    public List<String> createBehaviorRebateOrder(BehaviorEntity behaviorEntity) {

        // 1. 根据行为类型, 查询返利配置列表
        List<DailyBehaviorRebateVO> dailyBehaviorRebateVOS = repository.queryDailyBehaviorRebateConfig(behaviorEntity.getBehaviorTypeVO());
        // 2. 构建用户行为下单聚合对象
        List<String> orderIds = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregates = new ArrayList<>();
        for (DailyBehaviorRebateVO behaviorRebateVO : dailyBehaviorRebateVOS) { // 返利配置
            // 拼装业务ID；用户ID_返利类型_外部透彻业务ID
            String bizId = behaviorEntity.getUserId() + Constants.UNDERLINE + behaviorRebateVO.getRebateType() + Constants.UNDERLINE + behaviorEntity.getOutBusinessNo();
            // 用户行为订单实体
            BehaviorRebateOrderEntity orderEntity = BehaviorRebateOrderEntity.builder()
                    .userId(behaviorEntity.getUserId())
                    .behaviorType(behaviorRebateVO.getBehaviorType())
                    .rebateDesc(behaviorRebateVO.getRebateDesc())
                    .rebateType(behaviorRebateVO.getRebateType())
                    .rebateConfig(behaviorRebateVO.getRebateConfig())
                    .outBusinessNo(behaviorEntity.getOutBusinessNo())
                    .bizId(bizId)
                    .orderId(RandomStringUtils.randomNumeric(12))
                    .build();

            // 构建用户行为消息体
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> sendMsg = sendRebateMessageEvent.buildEventMessage(
                    SendRebateMessageEvent.RebateMessage.builder()
                    .userId(behaviorEntity.getUserId())
                    .rebateType(behaviorRebateVO.getRebateType())
                    .rebateDesc(behaviorRebateVO.getRebateDesc())
                    .rebateConfig(behaviorRebateVO.getRebateConfig())
                    .bizId(bizId)
                    .build()
            );

            // task实体, 添加消息体
            TaskEntity taskEntity= TaskEntity.builder()
                    .userId(behaviorEntity.getUserId())
                    .topic(sendRebateMessageEvent.topic())
                    .messageId(sendMsg.getId())
                    .message(sendMsg)
                    .state(TaskStateVO.create)
                    .build();

            // 聚合对象
            BehaviorRebateAggregate behaviorRebateAggregate = BehaviorRebateAggregate.builder()
                    .userId(behaviorEntity.getUserId())
                    .behaviorRebateOrderEntity(orderEntity)
                    .taskEntity(taskEntity)
                    .build();

            // 添加orderId列表, 用户行为聚合对象列表
            orderIds.add(orderEntity.getOrderId());
            behaviorRebateAggregates.add(behaviorRebateAggregate);
        }

        // 3. 聚合对象入库, 并推送mq
        repository.saveUserRebateRecord(behaviorEntity.getUserId(), behaviorRebateAggregates);

        // 4. 返回orderIds列表
        return orderIds;
    }

    // 根据userId, outBusinessNo 查询 Behavior_order表, 返回实体
    @Override
    public List<BehaviorRebateOrderEntity> queryOrderByOutBusinessNo(String userId, String outBusinessNo) {
        return repository.queryOrderByOutBusinessNo(userId, outBusinessNo);
    }

}
